id | title | description | subtitle |
---|---|---|---|
serverless-drivers |
Serverless Drivers |
Connecting to your Postgres database in serverless environments. |
Connecting to your Postgres database in serverless environments. |
Supabase provides several options for connecting to your Postgres database from serverless environments.
supabase-js is an isomorphic JavaScript client that uses the auto-generated REST API and therefore works in any environment that supports HTTPS connections. This API has a built-in connection pooler and can serve thousands of simultaneous requests, and therefore is ideal for Serverless workloads.
Vercel's Edge runtime is built on top of the V8 engine, that provides a limited set of Web Standard APIs.
Choose one of these Vercel Deploy Templates which use our Vercel Deploy Integration to automatically configure your connection strings as environment variables on your Vercel project!
})}
In your Database Settings
, make sure Use connection pooler
is checked and Transaction
mode is selected, then copy the URI and save it as the POSTGRES_URL
environment variable. Remember to replace the password placeholder with your actual database password and add the following suffix ?workaround=supabase-pooler.vercel
.
POSTGRES_URL="postgres://postgres.cfcxynqnhdybqtbhjemm:[YOUR-PASSWORD]@aws-0-ap-southeast-1.pooler.supabase.com:6543/postgres?workaround=supabase-pooler.vercel"
import { pgTable, serial, text, timestamp, uniqueIndex } from 'drizzle-orm/pg-core'
import { InferSelectModel, InferInsertModel } from 'drizzle-orm'
import { sql } from '@vercel/postgres'
import { drizzle } from 'drizzle-orm/vercel-postgres'
export const UsersTable = pgTable(
'users',
{
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
image: text('image').notNull(),
createdAt: timestamp('createdAt').defaultNow().notNull(),
},
(users) => {
return {
uniqueIdx: uniqueIndex('unique_idx').on(users.email),
}
}
)
export type User = InferSelectModel<typeof UsersTable>
export type NewUser = InferInsertModel<typeof UsersTable>
// Connect to Vercel Postgres
export const db = drizzle(sql)
import { Generated, ColumnType } from 'kysely'
import { createKysely } from '@vercel/postgres-kysely'
interface UserTable {
// Columns that are generated by the database should be marked
// using the `Generated` type. This way they are automatically
// made optional in inserts and updates.
id: Generated<number>
name: string
email: string
image: string
// You can specify a different type for each operation (select, insert and
// update) using the `ColumnType<SelectType, InsertType, UpdateType>`
// wrapper. Here we define a column `createdAt` that is selected as
// a `Date`, can optionally be provided as a `string` in inserts and
// can never be updated:
createdAt: ColumnType<Date, string | undefined, never>
}
// Keys of this interface are table names.
export interface Database {
users: UserTable
}
export const db = createKysely<Database>()
export { sql } from 'kysely'
Cloudflare's Workers runtime also uses the V8 engine but provides polyfills for a subset of Node.js APIs and TCP Sockets API, giving you a couple of options:
Supabase Edge Functions uses the Deno runtime which has native support for TCP connections allowing you to choose your favorite client: